#ifndef _FINEGERPRINT_PROTOCOL_HERAD_H
#define _FINEGERPRINT_PROTOCOL_HERAD_H
#include <iostream>
#include <stdint.h>
#include <stdio.h>
#include <vector>

using namespace std;

#define PACKET_FIX_LEN 9

class FingerprintProtocol
{
public:
    typedef struct
    {
        uint8_t head[2];
        uint8_t addr[4];
        uint8_t pid;
        uint8_t len[2];
        uint8_t data[1];
    } packet_t;

    enum
    {
        COMMAND_PACKET = 0x01,   // 命令
        DATA_PACKET    = 0x02,   // 数据
        ACK_PACKET     = 0x07,   // 应答
        END_PACKET     = 0x08,   // 最后一个包结束包
    };

    FingerprintProtocol(const vector<uint8_t>& packet);
    FingerprintProtocol();
    void addPacketHead(const uint16_t head = 0xef01);
    void addPacketAddr(uint32_t addr = 0xFFFFFFFF);
    void addPacketID(uint8_t pid);
    void addPacketLen(uint16_t len);
    void addPacketContent(uint8_t* data, uint32_t size);
    void addPacketSum(void);
    void showPacket(void) const;

    uint16_t        getPacketHead(void) const;
    uint32_t        getPacketAddr(void) const;
    uint8_t         getPacketID(void) const;
    uint16_t        getPacketDataLen(void) const;
    uint8_t*        getPacketData(void) const;
    uint16_t        getPacketSum(void) const;
    bool            isPacketError(void);
    vector<uint8_t> getPacket(void) const;


    // 生成握手协议
    static FingerprintProtocol makeHandshakeProtocol(void);
    static FingerprintProtocol formProtocolPacket(const vector<uint8_t>& packet);
    static FingerprintProtocol makeDetectImageProtocol(void);
    static FingerprintProtocol makeImageFeatureProtocol(uint8_t bufferID);
    static FingerprintProtocol makeImageTemplateProtocol(void);
    static FingerprintProtocol makeStoreTemplateProtocol(uint16_t pos);
    static FingerprintProtocol makeSearchFingerprintProtocol(uint16_t startPos = 0,
                                                             uint16_t n        = 50);
    static FingerprintProtocol makeDeleteFingerprintProtocol(uint16_t pos);
    static FingerprintProtocol makeClearFingerprintLibProtocol(void);

private:
    vector<uint8_t> packet;
};

#endif